//
//  ImageCollectionViewController.swift
//  YueJian
//
//  Created by mac on 2017/10/20.
//  Copyright © 2017年 MapleStory. All rights reserved.
//

import UIKit
import Alamofire

private let reuseIdentifier = "Cell"

class ImageCollectionViewController: UICollectionViewController {


    var array:[Any]?
    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = falsecccc

        self.title = "晒图"
        self.collectionView?.backgroundColor = UIColor.white
 
        let sessionId = UserDefaults.standard.value(forKey: "sessionId")
        
        print(sessionId!)
        let params = ["SessionID":sessionId ?? "","mod":"shaitu","related":1] as [String : Any]
        print(params)
        
        Alamofire.request(URL(string:"http://172.24.10.175/workout/api.php/lists")!, method: HTTPMethod.post, parameters: params, encoding: URLEncoding.default, headers: nil).responseJSON { (response) in
            print(response)
            self.array = response.result.value as? [Any]
            
            self.collectionView?.reloadData()
        }
        
        self.collectionView?.register(UINib(nibName: "CollectionViewCell", bundle: nil), forCellWithReuseIdentifier: reuseIdentifier)

        
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */

    // MARK: UICollectionViewDataSource

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return self.array?.count ?? 0
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CollectionViewCell
     
        if array?.count != 0 {
            let dic = array?[indexPath.row] as! [String:AnyObject]
            print(dic["thumb0"]!)
            cell.dic = dic
        }
//        if array.count != 0 {
//            let str =  dic["thumb"] as! String
//            let imageStr = "http://172.24.10.175/workout/Uploads/"+str
//            let images = try! UIImage(data: Data(contentsOf: URL(string: imageStr)!))
//            cell.backgroundView = UIImageView(image: images)
//
//        }
        
        cell.backgroundColor = UIColor.white
        return cell
    }
    
    
    
    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        let vc = IageDetailTableViewController()
        vc.title = "晒图"
        vc.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(vc, animated: true)
    }

    // MARK: UICollectionViewDelegate

    /*
    // Uncomment this method to specify if the specified item should be highlighted during tracking
    override func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
        return true
    }
    */

    /*
    // Uncomment this method to specify if the specified item should be selected
    override func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
        return true
    }
    */

    /*
    // Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
    override func collectionView(_ collectionView: UICollectionView, shouldShowMenuForItemAt indexPath: IndexPath) -> Bool {
        return false
    }

    override func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
        return false
    }

    override func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) {
    
    }
    */

}
